package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.MachOilAddDel;
import com.whfc.mach.dto.AppMachOilRecordDTO;
import com.whfc.mach.entity.AppDeviceObdLog;
import com.whfc.mach.param.*;
import com.whfc.ms.api.mach.service.MsMachService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-11
 */
@RequiresPermissions("mach/list")
@RestController
@RequestMapping("/ms/api/v1/mach")
@Validated
public class MsMachController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachService msMachService;

    @RequiresPermissions("mach/list/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody MachAddParam param) {
        logger.info("添加设备,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/list/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody MachEditParam param) {
        logger.info("编辑设备,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachService.edit(param);
        return ResultUtil.success();
    }

    @PostMapping("/editMachTank")
    public Result editMachTank(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody MachTankEditParam param) {
        logger.info("编辑设备油箱,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachService.editMachTank(param);
        return ResultUtil.success();
    }

    @PostMapping("/export/{deptId}")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("deptId") Integer deptId) {
        logger.info("导出设备信息,loginer:{},deptId:{}", user.getUsername(), deptId);
        OssPathDTO path = msMachService.export(deptId);
        return ResultUtil.success(path);
    }

    @PostMapping("/obd/export/{deptId}")
    public Result exportObd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("deptId") Integer deptId) {
        logger.info("导出电力设备信息,loginer:{},deptId:{}", user.getUsername(), deptId);
        OssPathDTO path = msMachService.exportObd(deptId);
        return ResultUtil.success(path);
    }

    @PostMapping("/enter/{machId}")
    public Result enter(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("machId") Integer machId) {
        logger.info("设备进场,loginer:{},machId:{}", user.getUsername(), machId);
        msMachService.enter(machId);
        return ResultUtil.success();
    }

    @PostMapping("/outer/{machId}")
    public Result outer(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("machId") Integer machId) {
        logger.info("设备退场,loginer:{},machId:{}", user.getUsername(), machId);
        msMachService.outer(machId);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/list/del")
    @PostMapping("/del/{machId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("machId") Integer machId) {
        logger.info("删除设备,loginer:{},machId:{}", user.getUsername(), machId);
        msMachService.del(machId);
        return ResultUtil.success();
    }

    /**
     * 绑定硬件
     *
     * @return
     */
    @RequiresPermissions("mach/list/bind")
    @PostMapping("/bind")
    public Result bind(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody DeviceMachBindParam request) {
        logger.info("设备绑定硬件,loginUser:{},参数：{}", user.getUsername(), request.toString());
        msMachService.bind(user, request);
        return ResultUtil.success();
    }

    /**
     * 解绑
     *
     * @param user
     * @param machId
     * @return
     */
    @RequiresPermissions("mach/list/unbind")
    @PostMapping("/unbind/{machId}")
    public Result unbind(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("machId") Integer machId) {
        logger.info("设备解除绑定硬件,loginUser:{},machId：{}", user.getUsername(), machId);
        msMachService.unbind(user, machId);
        return ResultUtil.success();
    }


    /**
     * 上传设备图片到服务器
     *
     * @param user
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadImg")
    public Result uploadImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user, MultipartFile file) throws IOException {
        logger.info("后台上传设备图片到服务器，loginer:{}", user.getUsername());
        OssPathDTO data = msMachService.uploadImg(file);
        return ResultUtil.success(data);
    }


    @Deprecated
    @PostMapping("/setOilAdd")
    public Result setOilAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody MachSetOilAddParam param) {
        logger.info("标定设备加油量,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachService.setOilAdd(param);
        return ResultUtil.success();
    }

    @Deprecated
    @GetMapping("/getOilAdd")
    public Result getOilAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取标定设备加油量详情,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        ListData<MachOilAdd> listData = msMachService.getOilAdd(machId, date);
        return ResultUtil.success(listData);
    }

    @PostMapping("/add/obd/data")
    public Result addObdData(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody AppDeviceObdLog appDeviceObdLog) {
        logger.info("添加设备,loginer:{},param:{}", user.getUsername(), appDeviceObdLog.toString());
        msMachService.addObdData(appDeviceObdLog);
        return ResultUtil.success();
    }

    @GetMapping(value = "/oil/add/list", params = {"deptId"})
    public Result getOilAddRecord(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate, @RequestParam(value = "keyword", required = false) @Length(max = 32) String keyword, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询项目加油记录,deptId:{},startDate:{},endDate:{},keyword:{},pageNum:{},pageSize:{}", deptId, startDate, endDate, keyword, pageNum, pageNum);
        PageData<AppMachOilRecordDTO> data = msMachService.getOilAddRecord(deptId, startDate, endDate, keyword, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/oil/add/list", params = {"machId"})
    public Result getOilAddRecord(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("machId") Integer machId, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询设备加油记录,machId:{},startDate:{},endDate:{},pageNum:{},pageSize:{}", machId, startDate, endDate, pageNum, pageSize);
        PageData<AppMachOilRecordDTO> data = msMachService.getOilAddRecord(machId, startDate, endDate, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("mach/data/addOil")
    @PostMapping(value = "oil/add/manual")
    public Result addOilAddManual(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated MachOilAddManual param) {
        logger.info("设备加油人工登记,user:{},param:{}", user.getUsername(), param);
        msMachService.addOilManual(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/oil/allButton")
    @PostMapping(value = "oil/add/adjust")
    public Result addOilAddAdjust(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated MachOilAddAdjust param) {
        logger.info("设备加油硬件校正,user:{},param:{}", user.getUsername(), param);
        msMachService.addOilAdjust(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/oil/allButton")
    @PostMapping(value = "oil/add/del")
    public Result delOilRecord(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated MachOilAddDel param) {
        logger.info("设备加油记录删除,user:{},param:{}", user.getUsername(), param);
        msMachService.delOilRecord(param);
        return ResultUtil.success();
    }
}
